#include<stdio.h>
#include<stdlib.h>

void countsort(int* arr, int n)
{
	int min = arr[0];
	int max = arr[0];
	for (int i = 0; i < n; i++)
	{
		if (arr[i] < min)
		{
			min = arr[i];
		}
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	int count = max - min + 1;
	int* tem = (int*)calloc(count,sizeof(int));
	int index = 0;
	for (int i = 0; i < n; i++)
	{
		tem[arr[i] - min]++;
	}
	for (int i = 0; i < count; i++)
	{
		while (tem[i]--)
		{
			arr[index++] = i + min;
		}
	}
	free(tem);
	tem = NULL;
}

int main() {
	int arr[] = { 4, 2, 2, 8, 3, 3, 1 };
	int n = sizeof(arr) / sizeof(arr[0]);

	countsort(arr, n);

	for (int i = 0; i < n; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");

	return 0;
}